/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.initilize;

import com.wtkj.exception.BusinessException;
import com.wtkj.vo.dzzf.base.kfwd.CardInfo;
import com.wttech.dzzf.process.dao.TransferUserInfoDao;
import com.wttech.dzzf.process.model.user.User;
import com.wttech.dzzf.process.model.user.UserCard;
import com.wttech.dzzf.process.service.user.UserDetailInfosCreatorService;
import org.apache.commons.lang.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 卡片信息处理服务实现
 * Created by MaShibo on 2017/10/23.
 */
@Service
public class CardInfoProcessService {

	private static final Logger LOGGER = LoggerFactory.getLogger(CardInfoProcessService.class);

	@Resource
	private TransferUserInfoDao transferUserInfoDao;

	@Resource
	private UserDetailInfosCreatorService userDetailInfosCreatorService;


	/**
	 * 处理卡片信息
	 *
	 * @param cardInfo dzzf卡片信息
	 * @param user     客户信息
	 */
	void processCardInfo(CardInfo cardInfo, User user) {
		Validate.notNull(cardInfo, "卡片信息不能为空");
		Validate.notNull(user, "客户信息不能为空");

		saveCardInfoUserId(user.getId(), cardInfo.getCardInfoId());

		userDetailInfosCreatorService.dealWithCard(user, cardInfo);
	}

	/**
	 * 保存卡片信息
	 *
	 * @param userId     客户信息id
	 * @param cardInfoId dzzf卡片信息id
	 */
	private void saveCardInfoUserId(String userId, String cardInfoId) {
		try {
			transferUserInfoDao.updateCardInfoUserIdbyId(userId, cardInfoId);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			LOGGER.error("更新卡片 {} 的 userId {} 失败", cardInfoId, userId);
			throw new BusinessException("更新卡片userId失败！");
		}
	}
}
